package com.uking.helper;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.uking.helper.VKPortalImple.initApplicationParameter;
import com.uking.moudle.IpInfo;
import com.uking.util.Constants;
import com.uking.util.TimeUtils;
import com.uking.util.Utils;
import com.uking.util.VKConstant;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class VKPortalProxy implements VKPortalInterface {
    private String baiduTime = null;
    private String currentTime = null;
    private String ip1;
    private String ip2;
    private boolean isNoResponse = true;
    private VKPortalInterface mVKPortalImple = new VKPortalImple();
    private String mMainAddress;
    private String mSecondAddress;
    private SharedPreferences sharedAAA;

    public VKPortalProxy(SharedPreferences sharedPre) {
        this.sharedAAA = sharedPre;
    }
        ////JIHUOMASUIBIANTIAN
    public String activateAuth(String authserver,String apktype,String activatecode) {
        if(mVKPortalImple!=null) {
            return mVKPortalImple.activateAuth(authserver,apktype,activatecode);
        }
        return null;
    }

    public String registerUser(String authserver, String uid, String apksn) {
        if(mVKPortalImple!=null) {
            return mVKPortalImple.registerUser(authserver,uid,apksn);
        }
        return null;
    }

    private boolean isGetNetTime(String strSN, String PackageName, SharedPreferences sharedPre, int type, int port, int count, initApplicationParameter initParams, String key) {
        String result = this.sharedAAA.getString(Constants.BEIJING_TIME, null);
        Utils.log("\u7f13\u5b58\u7684\u65f6\u95f4\u7c7b\u578b\u4e3a = " + result);
        if (TextUtils.isEmpty(result)) {
            result = Constants.baiduUrl;
        }
        if (result.equals(Constants.baiduUrl)) {
            return getTimeByType(strSN, PackageName, sharedPre, type, port, count, initParams, key, Constants.baiduUrl, Constants.googleUrl);
        }
        return getTimeByType(strSN, PackageName, sharedPre, type, port, count, initParams, key, Constants.googleUrl, Constants.baiduUrl);
    }

    private boolean getTimeByType(String strSN, String PackageName, SharedPreferences sharedPre, int type, int port, int count, initApplicationParameter initParams, String key, String webUrl1, String webUrl2) {
        this.baiduTime = TimeUtils.getBeiJingTime(webUrl1);
        if (TextUtils.isEmpty(this.baiduTime) || this.baiduTime.equals("1970-01-01 08:00:00")) {
            Utils.log("\u6839\u636e" + webUrl1 + "\u83b7\u53d6\u7684\u65f6\u95f4\u4e3a\u7a7a\uff0c\u73b0\u5728\u6839\u636e" + webUrl2 + "\u83b7\u53d6");
            this.baiduTime = TimeUtils.getBeiJingTime(webUrl2);
            Utils.log("\u5f53\u524d" + webUrl2 + "\u65f6\u95f4\u4e3a = " + this.baiduTime);
            if (TextUtils.isEmpty(this.baiduTime) || this.baiduTime.equals("1970-01-01 08:00:00")) {
                Utils.log("\u83b7\u53d6\u7f51\u7edc\u65f6\u95f4\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56defalse");
                Utils.log("\u65e0\u7f51\u7edc\uff0c\u9700\u8981\u8fd4\u56de\u9519\u8bef\u7801\u4e8690001");
                VKErrorMessageUtil.setMessageInfo(VKConstant.NO_GET_NET_TIME);
                initParams.setRegisterState(Boolean.valueOf(false));
                return false;
            }
            this.sharedAAA.edit().putString(Constants.BEIJING_TIME, webUrl2).commit();
            Utils.log("\u6839\u636e" + webUrl2 + "\u83b7\u53d6\u7684\u65f6\u95f4 = " + this.baiduTime);
            return isThan24Hours(strSN, PackageName, sharedPre, type, port, count, initParams, key);
        }
        this.sharedAAA.edit().putString(Constants.BEIJING_TIME, webUrl1).commit();
        Utils.log("\u6839\u636e" + webUrl1 + "\u83b7\u53d6\u7684\u65f6\u95f4 = " + this.baiduTime);
        return isThan24Hours(strSN, PackageName, sharedPre, type, port, count, initParams, key);
    }

    private boolean isThan24Hours(String strSN, String PackageName, SharedPreferences sharedPre, int type, int port, int count, initApplicationParameter initParams, String key) {
        String lastTime = this.sharedAAA.getString(Constants.NEW_REGISTER_CACHE_TIME, null);
        Utils.log("\u4e0a\u6b21\u6ce8\u518c\u6210\u529f\u7684\u7f13\u5b58\u65f6\u95f4=" + lastTime);
        if (TextUtils.isEmpty(lastTime)) {
            Utils.log("\u7f13\u5b58\u65f6\u95f4\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u8fd4\u56de\u9519\u8bef\u7801\u4e8690000");
            VKErrorMessageUtil.setMessageInfo(VKConstant.NO_MORE_THAN_SET_TIME);
            initParams.setRegisterState(Boolean.valueOf(false));
            return false;
        }
        Utils.log("\u5f53\u524d\u65f6\u95f4=" + this.baiduTime);
        boolean isThan24 = TimeUtils.isThan24Hours(lastTime, this.baiduTime);
        Utils.log("\u662f\u5426\u8d85\u8fc724\u5c0f\u65f6=" + isThan24);
        if (isThan24) {
            this.isNoResponse = true;
            return registerUserByDefault(strSN, PackageName, sharedPre, type, port, initParams, key);
        }
        Utils.log("\u7f13\u5b58\u65f6\u95f4\u6ca1\u6709\u8d85\u8fc724\u5c0f\u65f6\uff0c\u9700\u8981\u8fd4\u56de\u9519\u8bef\u7801\u4e8690000");
        VKErrorMessageUtil.setMessageInfo(VKConstant.NO_MORE_THAN_SET_TIME);
        initParams.setRegisterState(Boolean.valueOf(false));
        return false;
    }

    private boolean isGet3ARegisterAddress(String strSN, String PackageName, SharedPreferences sharedPre, int type, int port, int count, initApplicationParameter initParams, String key) {
        JSONObject result = null;
        String address = this.sharedAAA.getString(Constants.GROUPING_ADDRESS, null);
        Utils.log("\u4ece\u7f13\u5b58\u62ff\u5230\u76843A\u5206\u7ec4\u5730\u5740=" + address);
        address = address.substring(Constants.IP_PORT_LIST.length() + 6, address.length() - 3);
        String address1 = address.split(",")[0].trim();
        String address2 = address.split(",")[1].trim();
        Utils.log("---------------address1=" + address1);
        Utils.log("---------------address2=" + address2);
        try {
            if (TextUtils.isEmpty(address1)) {
                Utils.log("\u4e3b\u57df\u540d\u4e0d\u5b58\u5728");
                throw new NullPointerException("mainAddress is null");
            }
            result = Utils.getGrouping3AaddressFromNet(strSN, address1);
            Utils.log("\u4ece\u4e3b\u57df\u540d\u83b7\u53d6\u6570\u636e\u5b8c\u6210 " + result.toString());
            return deal3ARegisterResult(address, result, strSN, PackageName, sharedPre, type, port, count, initParams, key);
        } catch (Exception e1) {
            e1.printStackTrace();
            Utils.log("\u4e3b\u5730\u5740\u5f02\u5e38 " + e1.getMessage());
            try {
                result = Utils.getGrouping3AaddressFromNet(strSN, address2);
                Utils.log("\u4e3b\u57df\u540d\u5f02\u5e38\uff0c\u4ece\u5907\u7528\u5730\u5740\u83b7\u53d6\u6570\u636e\u5b8c\u6210" + result.toString());
            } catch (Exception e) {
                e.printStackTrace();
                Utils.log("\u5907\u7528\u7684\u5730\u5740\u4e5f\u5931\u8d25\u4e86\uff01");
            }
        }
        return true;
    }

    private boolean deal3ARegisterResult(String cacheAddress, JSONObject result, String strSN, String PackageName, SharedPreferences sharedPre, int type, int port, int count, initApplicationParameter initParams, String key) {
        if (result == null) {
            return isGetNetTime(strSN, PackageName, sharedPre, type, port, count, initParams, key);
        }
        try {
            Utils.log("\u5f00\u59cb\u5904\u7406\u7ed3\u679c " + result.toString());
            int status = result.getInt("status");
            Utils.log("status== " + status);
            Utils.log("\u5224\u65ad\u670d\u52a1\u5668\u4e2d\u8fd4\u56de\u7684\u503c\u4e2d\u662f\u5426\u5305\u542b\u65f6\u95f4\u8fd9\u4e2a\u5b57\u6bb5");
            if (result.toString().contains("current_time")) {
                Utils.log("\u5305\u542b");
                this.currentTime = result.getString("current_time");
            } else {
                Utils.log("\u4e0d\u5305\u542b");
            }
            Utils.log("currentTime== " + this.currentTime);
            if (status == 0) {
                Utils.log("\u6210\u529f\u62ff\u5230\u5206\u7ec43A\u5730\u5740");
                String AAAaddress = result.getString("aaa");
                Utils.log("\u5206\u7ec43A\u5730\u5740=" + AAAaddress);
                Utils.set3AAddressCache(cacheAddress, AAAaddress, this.sharedAAA);
                AAAaddress = this.sharedAAA.getString(Constants.GROUPING_ADDRESS, null);
                AAAaddress = AAAaddress.substring(Constants.IP_PORT_LIST.length() + 6, AAAaddress.length() - 3);
                Utils.log("AAAaddress==" + AAAaddress);
                ArrayList<IpInfo> list = Utils.getResolveAddress(AAAaddress);
                Utils.log("\u5f00\u59cb\u68c0\u67e53A\u6ce8\u518c\u662f\u5426\u6210\u529f");
                String address1 = ((IpInfo) list.get(0)).getIp().trim();
                String address2 = ((IpInfo) list.get(1)).getIp().trim();
                Utils.log("\u7b2c\u4e00\u4e2a\u5730\u5740\u662f=" + address1);
                Utils.log("\u7b2c\u4e8c\u4e2a\u5730\u5740\u662f=" + address2);
                this.ip1 = Utils.domain2ip(address1);
                Utils.log("ip1=" + this.ip1);
                if (!TextUtils.isEmpty(this.ip1)) {
//                    if (this.mVKPortalImple.registerUser(strSN, this.ip1, PackageName, sharedPre, type, port, 1, initParams, key)) {
//                        Utils.log("\u65b0\u5730\u57401\u6ce8\u518c\u7ed3\u679c\uff1a3A\u6ce8\u518c\u5df2\u7ecf\u6210\u529f\uff0c\u8bf7\u7ee7\u7eed\u5b8c\u6210\u83b7\u53d6\u8282\u76ee\u5355\u7b49\u540e\u7eed\u64cd\u4f5c");
//                        if (!TextUtils.isEmpty(this.currentTime)) {
//                            Utils.log("\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684\u7f51\u7edc\u65f6\u95f4\u4e0d\u4e3a\u7a7a\uff0c\u628a\u65f6\u95f4\u7f13\u5b58\u8d77\u6765");
//                            TimeUtils.setBJCacheTime(this.sharedAAA, this.currentTime);
//                        }
//                        return true;
//                    } else
                        if (need_backup_register(strSN)) {
                        Utils.log("\u65b0\u5730\u57401\u6ce8\u518c\u5931\u8d25\uff0c\u9700\u8981\u4f7f\u7528\u65b0\u5730\u57402\u6ce8\u518c");
                    } else {
                        Utils.log("\u65b0\u5730\u57401\u6ce8\u518c\u5931\u8d25\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u65b0\u5730\u57402\u6ce8\u518c");
                        return new VKPortalImple().whether_can_cached(sharedPre, type, port, initParams, key);
                    }
                }
                this.ip2 = Utils.domain2ip(address2);
                Utils.log("ip2=" + this.ip2);
                Utils.log("\u65b0\u5730\u57401\u6ce8\u518c\u7ed3\u679c\uff1a3A\u6ce8\u518c\u5931\u8d25\uff0c\u4f7f\u7528\u65b0\u5730\u57402\u6ce8\u518c");
                if (TextUtils.isEmpty(this.ip2)) {
                    Utils.log("\u7b2c\u4e8c\u4e2a\u5730\u5740\u4e0d\u5b58\u5728");
                    return false;
                }
//                if (this.mVKPortalImple.registerUser(strSN, this.ip2, PackageName, sharedPre, type, port, 2, initParams, key)) {
//                    Utils.log("\u65b0\u5730\u57402\u6ce8\u518c\u7ed3\u679c\uff1a3A\u6ce8\u518c\u5df2\u7ecf\u6210\u529f\uff0c\u8bf7\u7ee7\u7eed\u5b8c\u6210\u83b7\u53d6\u8282\u76ee\u5355\u7b49\u540e\u7eed\u64cd\u4f5c");
//                    if (!TextUtils.isEmpty(this.currentTime)) {
//                        Utils.log("\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684\u7f51\u7edc\u65f6\u95f4\u4e0d\u4e3a\u7a7a\uff0c\u628a\u65f6\u95f4\u7f13\u5b58\u8d77\u6765");
//                        TimeUtils.setBJCacheTime(this.sharedAAA, this.currentTime);
//                    }
//                    return true;
//                }
                Utils.log("\u65b0\u5730\u57402\u6ce8\u518c\u7ed3\u679c\uff1a3A\u6ce8\u518c\u5931\u8d25");
                return false;
            } else if (1 == status) {
                Utils.log("\u9519\u8bef1\uff0c\u9700\u8981\u8fd4\u56de\u9519\u8bef\u7801\u4e8620000");
                VKErrorMessageUtil.setMessageInfo(20000);
                initParams.setRegisterState(Boolean.valueOf(false));
                return false;
            } else {
                this.isNoResponse = false;
                return registerUserByDefault(strSN, PackageName, sharedPre, type, port, initParams, key);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }
    }

    private boolean registerUserByDefault(String strSN, String PackageName, SharedPreferences sharedPre, int type, int port, initApplicationParameter initParams, String key) {
        Utils.cacheDefault3AAddress(this.mMainAddress, this.mSecondAddress, 8095, this.sharedAAA);
        Utils.log("\u5c06\u4e3b\u5907\u57df\u540d\u8f6c\u5316\u4e3aIP\u5730\u5740");
        this.ip1 = Utils.domain2ip(this.mMainAddress);
        boolean result = false;
        if (!TextUtils.isEmpty(this.ip1)) {
            Utils.log("\u4f7f\u7528\u9ed8\u8ba4\u4e3b\u5730\u5740\u8fdb\u884c\u6ce8\u518c ip1=" + this.ip1);
            ///result = this.mVKPortalImple.registerUser(strSN, this.ip1, PackageName, sharedPre, type, port, 1, initParams, key);
            Utils.log("\u4e3b\u9ed8\u8ba4\u5730\u5740\u6ce8\u518c  " + result);
        }
        if (result) {
            Utils.log("\u6ce8\u518c\u6210\u529f");
            if (this.isNoResponse) {
                Utils.log("\u4ece\u65e0\u54cd\u5e94\u72b6\u6001\u8fdb\u5165\u5185\u7f6e\u7684\u57df\u540d\u8fdb\u884c\u6ce8\u518c\uff0c\u7f13\u5b58\u83b7\u53d6\u7684\u7f51\u7edc\u65f6\u95f4=" + this.baiduTime);
                TimeUtils.setBJCacheTime(this.sharedAAA, this.baiduTime);
            } else {
                Utils.log("\u4ece\u6ca1\u6709\u5206\u7ec4\u5730\u5740\u8fdb\u5165\u5185\u7f6e\u7684\u5206\u7ec4\u5730\u5740\u8fdb\u884c\u6ce8\u518c");
                Utils.log("\u5224\u65ad\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684\u65f6\u95f4\u662f\u5426\u4e3a\u7a7a");
                if (TextUtils.isEmpty(this.currentTime)) {
                    Utils.log("\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684\u7f51\u7edc\u65f6\u95f4\u4e3a\u7a7a\uff0c\u4e0d\u66f4\u65b0\u65f6\u95f4");
                } else {
                    Utils.log("\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684\u7f51\u7edc\u65f6\u95f4\u4e0d\u4e3a\u7a7a\uff0c\u628a\u65f6\u95f4\u7f13\u5b58\u8d77\u6765");
                    TimeUtils.setBJCacheTime(this.sharedAAA, this.currentTime);
                }
            }
            return true;
        }
        if (need_backup_register(strSN)) {
            Utils.log("\u5185\u7f6e\u4e3b\u57df\u540d\u6ce8\u518c\u5931\u8d25\uff0c\u9700\u8981\u4f7f\u7528\u9ed8\u8ba4\u5907\u7528\u5730\u5740\u8fdb\u884c\u6ce8\u518c");
            this.ip2 = Utils.domain2ip(this.mSecondAddress);
            if (!TextUtils.isEmpty(this.ip2)) {
                Utils.log("\u4f7f\u7528\u9ed8\u8ba4\u5907\u7528\u5730\u5740\u8fdb\u884c\u6ce8\u518c  ip2=" + this.ip2);
               /// result = this.mVKPortalImple.registerUser(strSN, this.ip2, PackageName, sharedPre, type, port, 2, initParams, key);
                Utils.log("\u5907\u7528\u9ed8\u8ba4\u5730\u5740\u6ce8\u518c " + result);
            }
            if (result) {
                if (this.isNoResponse) {
                    Utils.log("\u4ece\u65e0\u54cd\u5e94\u72b6\u6001\u8fdb\u5165\u5185\u7f6e\u7684\u57df\u540d\u8fdb\u884c\u6ce8\u518c\uff0c\u7f13\u5b58\u83b7\u53d6\u7684\u7f51\u7edc\u65f6\u95f4=" + this.baiduTime);
                    TimeUtils.setBJCacheTime(this.sharedAAA, this.baiduTime);
                } else {
                    Utils.log("\u4ece\u6ca1\u6709\u5206\u7ec4\u5730\u5740\u8fdb\u5165\u5185\u7f6e\u7684\u5206\u7ec4\u5730\u5740\u8fdb\u884c\u6ce8\u518c");
                    Utils.log("\u5224\u65ad\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684\u65f6\u95f4\u662f\u5426\u4e3a\u7a7a");
                    if (TextUtils.isEmpty(this.currentTime)) {
                        Utils.log("\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684\u7f51\u7edc\u65f6\u95f4\u4e3a\u7a7a\uff0c\u4e0d\u66f4\u65b0\u65f6\u95f4");
                    } else {
                        Utils.log("\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684\u7f51\u7edc\u65f6\u95f4\u4e0d\u4e3a\u7a7a\uff0c\u628a\u65f6\u95f4\u7f13\u5b58\u8d77\u6765");
                        TimeUtils.setBJCacheTime(this.sharedAAA, this.currentTime);
                    }
                }
            }
        } else {
            Utils.log("\u5185\u7f6e\u4e3b\u57df\u540d\u6ce8\u518c\u5931\u8d25\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u9ed8\u8ba4\u5907\u7528\u5730\u5740\u8fdb\u884c\u6ce8\u518c");
            result = new VKPortalImple().whether_can_cached(sharedPre, type, port, initParams, key);
        }
        return result;
    }

    private boolean need_backup_register(String sn) {
        if (VKConstant.strAAARegisterInfo == null || !VKConstant.strAAARegisterInfo.contains("ERROR_CODE")) {
            return true;
        }
        return VKErrorMessageUtil.saveMianErrorMsge(VKConstant.strAAARegisterInfo, sn);
    }
}
